/**
 * Created by Administrator on 2015/4/21.
 */
(function(){

	var Class = (function(){
		var Class = function(){};
		var _mix = function(r,p){
			for(var i in p){
				if(p.hasOwnProperty(i)) r[i] = p[i];
			}
		};
		var _extend = function(){
			this.initProperty = true;
			var prototype = new this();
			this.initProperty = false;
			var items = [].slice.call(arguments);
			var item;
			while(item=items.shift()){
				_mix(prototype,item.prototype || item);
			}
			function SubClass(){
				if(!SubClass.initProperty && this.initialize){
					this.initialize.apply(this,arguments);
				}
			}
			SubClass.prototype = prototype;
			SubClass.prototype.constructor = SubClass;
			SubClass.extend = _extend;
			return SubClass;
		};

		Class.extend = _extend;
		return Class;

	})();
	//观察者
	var Obser = (function(){
		var _indexOf = function(array,item){
			if (array === null) return -1;
			var i = 0, length = array.length;
			for (; i < length; i++) if (array[i] === item) return i;
			return -1;
		};
		var Obser = {
			on : function(type,listener){
				this.__listeners = this.__listeners || {};
				this.__listeners[type] = this.__listeners[type] || [];
				if((_indexOf(this.__listeners,type)===-1) && (typeof listener==="function")){
					this.__listeners[type].push(listener);
				}
				return this;
			},
			fire : function(type){
				if(!this.__listeners || !this.__listeners[type]) return false;
				var listeners = this.__listeners[type];
				var args = Array.prototype.slice.call(arguments,1);
				this.__types = this.__types || [];
				if((_indexOf(this.__types,type)===-1)) this.__types.push(type);
				for(var i= 0,len=listeners.length; i<len; i++){
					listeners[i].apply(this,args);
				}
				return this;
			},
			off : function(type,listener){
				if(!type && !listener) this.__listeners={};
				if(type && !listener) delete this.__listeners[type];
				this.__types = this.__types || [];
				var tindex = _indexOf(this.__types,type);
				if(tindex>-1) this.__types.splice(tindex,1);
				if(type && listener){
					var listeners = this.__listeners[type];
					var index = _indexOf(listeners,listener);
					(index>-1) && listeners.splice(index,1);
				}
				return this;
			}
		};
		return Obser;
	})();
	//组件基类
	var BaseClass = Class.extend(Obser,{
		template : "",
		//此方法不可覆盖
		initialize : function(config){
			var that = this;
			config = config || {};
			this._config = config;
			this.init(this._config);
		},
		parseTemplate:function(str,data){
			/**
			 * http://ejohn.org/blog/javascript-micro-templating/
			 * https://github.com/jashkenas/underscore/blob/0.1.0/underscore.js#L399
			 */
			var fn = new Function('obj',
				'var p=[],print=function(){p.push.apply(p,arguments);};' +
				'with(obj){p.push(\'' + str
					.replace(/[\r\t\n]/g, " ")
					.split("<%").join("\t")
					.replace(/((^|%>)[^\t]*)'/g, "$1\r")
					.replace(/\t=(.*?)%>/g, "',$1,'")
					.split("\t").join("');")
					.split("%>").join("p.push('")
					.split("\r").join("\\'") +
				"');}return p.join('');")
			return data ? fn(data) : fn
		},
		get : function(key){
			return this._config[key];
		},
		set : function(key,val){
			this._config[key] = val;
		},
		//专门提供给子类覆盖 用于子类初始化
		init : function(config){}
	})

	if(!window.BaseClass) window.BaseClass = BaseClass;


})();